<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select clearable v-model="filters.state" placeholder="请选择">
            <el-option label="已激活" value="1"></el-option>
            <el-option label="未激活" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="queryList">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleExport">导出</el-button>
        </el-form-item>

        <el-form-item label="" prop="resource">
          <el-col :span="12">
            <el-upload
                class="upload-demo"
                action="http://localhost:8080/employee/importExcel"
                list-type="picture">
              <el-button  type="primary">点击导入</el-button>
            </el-upload>
          </el-col>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="用户名称" width="150" sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="100" sortable>
      </el-table-column>
      <el-table-column prop="state" label="账号状态" width="100" sortable>
        <template scope="scope"><!--scope有整行数据-->
          <div v-if="scope.row.state == 4" slot="reference" class="name-wrapper">
            <el-tag type="danger" size="medium">未激活</el-tag>
          </div>
          <div v-if="scope.row.state == 1" slot="reference" class="name-wrapper">
            <el-tag type="success" size="medium">正常</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="shop.name" label="所属店铺"  width="100" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="手机号码" width="150"  sortable>
      </el-table-column>
      <el-table-column prop="email" label="电子邮件" width="200"  sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!--
        :disabled===0保持禁止状态this.sels.length勾选框不等于0
      -->
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :page-size="pageSize"
          :page-sizes="pageSizes"
          :current-page="currentPage"
          :total="total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="用户" prop="username">
          <el-input v-model="editForm.username" placeholder="请输入用户名称" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="editForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="editForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="editForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="editForm.state">
            <el-radio class="radio" :label="1">已激活</el-radio>
            <el-radio class="radio" :label="4">未激活</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
//import util from '../../common/js/util'
//import NProgress from 'nprogress'
//import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

export default {
  data() {
    return {
      //部门树
      departmentTree:[],
      //员工列表数据
      employees:[],
      //分页条件
      pageSizes:[5, 10, 15, 30],
      total: 0,
      currentPage: 1,
      pageSize:10,

      filters: {
        keyword: '',
        state:''
      },
      //部门数据列表
      departments: [],
      //列表选中列
      sels: [],

      listLoading: false,
      //编辑修改专用
      editFormVisible: false,//编辑界面是否显示
      title:'',//弹出框的标题
      editLoading: false,
      //验证提示
      editFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      //编辑界面数据
      editForm: {
        id:null,
        state:0,
        username:'',
        email:'',
        phone:'',
        password:'',
        age:'',
      }

    }
  },
  methods: {
    //导出文件
    handleExport(){
      location.href = "http://localhost:8080/employee/export";
    },

    handleSizeChange(val){
      this.pageSize = val;
      this.getEmployees();
    },
    //当前页改变
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getEmployees();
    },
    //查询
    queryList(){
      this.currentPage = 1;
      this.getEmployees();
    },
    //获取用户列表
    getEmployees() {
      let para = {
        //所需参数
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.filters.keyword,
        state: this.filters.state
      };
      this.listLoading = true;
      //获取部门列表数据
      this.$http.post("/employee/pageList",para).then(res =>{
        this.employees = res.data.data;
        this.total = res.data.total;
        this.listLoading = false;//关闭忙等
      })
    },
    // getDepartmentTree(){
    //   this.$http.get("/department/tree").then(res =>{
    //     this.departmentTree = res.data;
    //   })
    // },
    //删除
    handleDel(index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'error'//提示图标
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/employee/"+row.id).then(res =>{
          this.listLoading = false;
          //判断是否删除成功
          if(res.data.success){
            //弹出提示框
            this.$message({
              message:res.data.msg,
              type:'success',
            })
            //跳转第一页
            this.currentPage = 1;
            //刷新列表
            this.getEmployees();
          }else {
            //删除失败
            this.$message({
              message:res.data.msg,
              type:'warning',
            })
          }
        })
        });
    },
    //显示编辑界面
    handleEdit(index, row) {
      //重置表单验证
      if(this.$refs['editForm']){
        this.$refs['editForm'].resetFields();//重置表单验证
      }
      this.title ='编辑'
      this.editForm = Object.assign({}, row);//克隆数据
      this.editFormVisible = true; //是否显示
    },
    //显示新增界面数据
    handleAdd(){
      //重置表单验证
      if(this.$refs['editForm']){
        this.$refs['editForm'].resetFields();//重置表单验证
      }
      this.title ='新增'
      //置空表单
      this.editForm = {
        id:null,
        sex:0,
        username:'',
        email:'',
        phone:'',
        salt:'',
        password:'',
        age:'',
        shop_id:null,

      }
      this.editFormVisible =true;
    },
    //提交
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '温馨提示', {}).then(() => {
            this.editLoading = true;
            //克隆表单数据
            let para = Object.assign({}, this.editForm);
            console.log(para)
            this.$http.put("/employee",para).then(res =>{
              if(res.data.success){
                //关闭忙等
                this.editLoading = false;
                this.$message({
                  message:res.data.msg,
                  type:'success'
                })
                //重置表单验证
                this.$refs['editForm'].resetFields();
                this.editFormVisible = false;//关闭模态框
                this.getEmployees();//刷新列表
              }else {
                this.$message({
                  message:res.data.msg,
                  type:'warning'
                })
                this.editLoading =false;//忙等关闭
              }
            })
          });
        }
      });
    },
    //批量删除勾选所选中的值
    selsChange: function (sels) {
      //console.log(sels)将所有选中的行赋值给
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      //返回一个id数组
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;//开启忙等
        this.$http.patch("/employee/del", ids).then(res =>{
              this.listLoading = false;
          //判断是否删除成功
          if(res.data.success){
            //弹出提示框
            this.$message({
              message:res.data.msg,
              type:'success',
            })
            //跳转第一页
            this.currentPage = 1;
            //刷新列表
            this.getEmployees();
          }else {
            //删除失败
            this.$message({
              message:res.data.msg,
              type:'warning',
            })
          }
        })
      }).catch(() => {

      });
    }
  },
  //钩子函数初始化数据优先加载
  mounted() {
    this.getEmployees();
    // this.getDepartmentTree();
  }
}

</script>

<style scoped>

</style>